package com.xbongbong.saas.model.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.elasticsearch.model.PaasEsModel;
import com.xbongbong.paas.elasticsearch.pojo.dto.InsertDTO;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.help.subform.TransferSubFormHelper;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.businessdata.pojo.dto.FormatExplainDTO;
import com.xbongbong.pro.formdata.pojo.dto.SaasAnalysisDataDTO;
import com.xbongbong.saas.domain.dao.DistributorAccountDao;
import com.xbongbong.saas.domain.entity.DistributorAccountEntity;
import com.xbongbong.saas.domain.entity.ext.CustomerEntityExt;
import com.xbongbong.saas.enums.business.DistributorAccountEnum;
import com.xbongbong.saas.enums.business.DistributorEnum;
import com.xbongbong.saas.help.ShowFormatHelp;
import com.xbongbong.saas.model.CustomerModel;
import com.xbongbong.saas.model.DistributorAccountModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author bomfish
 */
@Service("distributorAccountModel")
public class DistributorAccountModelImpl implements DistributorAccountModel {

    @Resource
    private CustomerModel customerModel;
    @Resource
    private DistributorAccountDao distributorAccountDao;
    @Resource
    private TransferSubFormHelper transferSubFormHelper;
    @Resource
    private PaasEsModel paasEsModel;
    @Resource
    private ShowFormatHelp showFormatHelp;

    @Override
    public void insertBatch(List<DistributorAccountEntity> list) throws XbbException {
        List<InsertDTO> insertDTOList = new ArrayList<>();
        if (list.size() == 1){
            distributorAccountDao.insert(list.get(0));
        }else {
            distributorAccountDao.insertBatch(list);
        }
        List<PaasFormDataEntity> paasFormDataEntities = transferSubFormHelper.transferDistributorAccountToFormDataList(list);
        for (PaasFormDataEntity paasFormDataEntity : paasFormDataEntities) {
            insertDTOList.add(paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_DISTRIBUTOR_ACCOUNT));
        }
        if (list.size() == 1){
            paasEsModel.insert(insertDTOList.get(0));
        }else {
            paasEsModel.insertBatch(insertDTOList);
        }
    }

    @Override
    public Integer insert(DistributorAccountEntity entity) throws XbbException {
        insertBatch(Collections.singletonList(entity));
        return entity.getId().intValue();
    }

    @Override
    public List<DistributorAccountEntity> findEntitys(Map<String, Object> param) {
        return distributorAccountDao.findEntities(param);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return distributorAccountDao.getEntitiesCount(param);
    }

    @Override
    public List<DistributorAccountEntity> findEntitysWithoutSub(Map<String, Object> param) {
        return distributorAccountDao.findEntities(param);
    }

    @Override
    public Integer update(DistributorAccountEntity entity) throws XbbException {
        updateBatch(Collections.singletonList(entity));
        return null;
    }

    @Override
    public void updateBatch(List<DistributorAccountEntity> list) throws XbbException {
        distributorAccountDao.updateBatch(list);
        List<PaasFormDataEntity> paasFormDataEntities = transferSubFormHelper.transferDistributorAccountToFormDataList(list);
        List<InsertDTO> updateDTOList = new ArrayList<>();
        for (PaasFormDataEntity paasFormDataEntity : paasFormDataEntities) {
            updateDTOList.add(paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_DISTRIBUTOR_ACCOUNT));
        }
        paasEsModel.updateBatch(updateDTOList);
    }

    @Override
    public DistributorAccountEntity getByKey(Long key, String corpid) {
        return distributorAccountDao.getByKey(key, corpid);
    }

    @Override
    public String getPhoneByOpenId(String openId) {
        return distributorAccountDao.getPhoneByOpenId(openId);
    }

    @Override
    public Integer getMaxSort(String corpid, Long distributorId) {
        return distributorAccountDao.getMaxSort(corpid, distributorId);
    }

    @Override
    public List<DistributorAccountEntity> getByIdIn(String corpid, List<Long> idIn) {
        return distributorAccountDao.getByIdIn(corpid, idIn);
    }

    @Override
    public List<DistributorAccountEntity> getByDistributorIdIn(String corpid, List<Long> distributorIdIn) {
        return distributorAccountDao.getByDistributorIdIn(corpid, distributorIdIn);
    }

    @Override
    public boolean phoneRepeat(String corpid, String phone, Long dataId) {
        return !distributorAccountDao.phoneRepeat(corpid, phone, dataId).isEmpty();
    }

    @Override
    public List<DistributorAccountEntity> getAllDistributorUserList(String corpid, long distributorId, String role) {
        return distributorAccountDao.getAllDistributorUserList(corpid, distributorId, role);
    }

    @Override
    public List<Long> getDistributorAccountNum(String corpid) {
        List<DistributorAccountEntity> distributorAccountEntityList = distributorAccountDao.getDistributorAccountNum(corpid);
        List<Long> distributorAccountNumList = new ArrayList<>(distributorAccountEntityList.size());
        distributorAccountEntityList.forEach(i -> distributorAccountNumList.add(i.getDistributorId()));
        return distributorAccountNumList;
    }

    @Override
    public Boolean isExists(String corpid, String phone) {
        Integer count = distributorAccountDao.isExists(corpid, phone);
        return count != null && count > 0;
    }

    @Override
    public void formatUpdateExplain(FormatExplainDTO formatExplainDTO) throws XbbException {
        for (FieldAttrEntity fieldAttrEntity : formatExplainDTO.getExplainList()) {
            String attr = fieldAttrEntity.getAttr();
            if (Objects.equals(attr, DistributorAccountEnum.PHONE.getAttr()) || Objects.equals(attr, DistributorAccountEnum.DISTRIBUTOR_NAME.getAttr())) {
                fieldAttrEntity.setEditable(0);
            }
        }
    }

    @Override
    public void setDistributorName(List<PaasFormDataEntityExt> paasFormDataESList, String corpid) {
        List<Long> idList = new ArrayList<>();
        paasFormDataESList.forEach(item -> idList.add(item.getData().getLong(DistributorAccountEnum.DISTRIBUTOR_NAME.getAttr())));
        List<CustomerEntityExt> customerEntityExts = customerModel.getByKeys(idList, corpid);
        Map<Long, String> nameMap = new HashMap<>(customerEntityExts.size());
        customerEntityExts.forEach(item -> nameMap.put(item.getId(), item.getData().getString(DistributorEnum.NAME.getAttr())));
        paasFormDataESList.forEach(item -> item.getData().put(DistributorAccountEnum.DISTRIBUTOR_NAME_LINK_TEXT.getAttr(), nameMap.get(item.getData().getLong(DistributorAccountEnum.DISTRIBUTOR_NAME.getAttr()))));
    }

    @Override
    public void formatData(SaasAnalysisDataDTO saasAnalysisDataDTO, JSONObject result) throws XbbException {
        Long distributorId = result.getLong(DistributorAccountEnum.DISTRIBUTOR_NAME.getAttr());
        CustomerEntityExt customerEntityExt = customerModel.getByKey(distributorId, saasAnalysisDataDTO.getCorpid());
        result.put(DistributorAccountEnum.DISTRIBUTOR_NAME_LINK_TEXT.getAttr(), customerEntityExt.getData().get(DistributorEnum.NAME.getAttr()));
        showFormatHelp.formatLinkBusiness4Show(result, DistributorAccountEnum.DISTRIBUTOR_NAME.getAttr(), DistributorAccountEnum.DISTRIBUTOR_NAME_LINK_TEXT.getAttr(), DistributorAccountEnum.DISTRIBUTOR_NAME.getFieldType());
    }
}
